
import os
import logging
from configparser import ConfigParser

class CfgReader():
    def __init__(self):
        self.ip = None
        self.username = None
        self.passwd = None
        self.localpaths = None
        self.remotepath = None
        self.traverse_interval = None
        self.project_name_rule = None
        self.project_check = None
        self.time_plan = None
        self.time_plan_enable = None

    def config_prase(self, cfg_file):
        if not os.path.exists(cfg_file):
            logging.error("config file %s not exist", cfg_file)
            return False
        try:
            parser = ConfigParser()
            parser.read(cfg_file, encoding="utf-8")
            self.cfg = dict(parser.items("fd_upload"))
            logging.debug("the config item: %s", self.cfg)
            self.ip = self.cfg['ip']
            self.username = self.cfg['username']
            self.passwd = self.cfg['passwd']
            localpath = self.cfg['localpath']
            self.localpaths = localpath.split(";")
            self.remotepath = self.cfg['remotepath']
            self.traverse_interval = self.cfg['traverse_interval']
            self.project_name_rule = self.cfg['project_name_rule']
            time_plan = self.cfg['time_plan']
            self.time_plan = time_plan.split(";")

            project_check = self.cfg['project_check']
            if project_check=="true":
                self.project_check = True
            else:
                self.project_check = False

            time_plan_enable = self.cfg['time_plan_enable']
            if time_plan_enable == "true":
                self.time_plan_enable = True
            else:
                self.time_plan_enable = False

            if not self.check_cfg():
                return False
            logging.info("prase %s success", cfg_file)
            return True
        except:
            return False
        
        
    def check_cfg(self):
        if self.ip is None or self.ip.strip() =='':
            return False
        if self.username is None or self.username.strip() =='':
            return False
        if self.passwd is None or self.passwd.strip() =='':
            return False
        if self.localpaths is None or len(self.localpaths) == 0:
            return False
        if self.remotepath is None or self.remotepath.strip() =='':
            return False
        
        return True
    

class Singleton(CfgReader):
    def foo(self):
        pass
cfg_reader = Singleton()